//------------------------------------------------------------------------------
// X2D Game Engine
// Copyright (C) 2006-2008 Janusz Kowalski
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

#include <cstdio>
#include <cstdarg>

#include "global.h"

FILE *logStream = 0;

const int PMSG_INFO = 0;
const int PMSG_WARNING = 1;
const int PMSG_ERROR = 2;

void pmsg(int type, const char *format, va_list args) {
  const char *prefix;
  switch(type) {
    case PMSG_INFO:
      prefix="INFO: "; break;
    case PMSG_WARNING:
      prefix="WARNING: "; break;
    case PMSG_ERROR:
      prefix="ERROR: "; break;
    default:
      prefix="UNKNOWN: "; break;
  }
  
  // Write INFOs to stdout, and ERRORs to stderr.
  if(type==PMSG_ERROR) {
    fprintf(stderr, prefix);
    vfprintf(stderr, format, args);
  } else {
    fprintf(stdout, prefix);
    vfprintf(stdout, format, args);
  }

  if(logStream!=0) {
    fprintf(logStream, prefix);
    vfprintf(logStream, format, args);
  }

  fflush(logStream);
}

// Waning message.
void pmsgWarningF(const char *format, ...) {
  va_list args;
  va_start(args, format);
  pmsg(PMSG_WARNING, format, args);
  va_end(args);
}

// Error message.
void pmsgErrorF(const char *format, ...) {
  va_list args;
  va_start(args, format);
  pmsg(PMSG_ERROR, format, args);
  va_end(args);
}

// Info message.
void pmsgInfoF(const char *format, ...) {
  va_list args;
  va_start(args, format);
  pmsg(PMSG_INFO,format,args);
  va_end(args);
}

// Debug message.
void pmsgDebugF(const char *format, ...) {
  va_list args;
#ifdef DEBUG
  va_start(args, format);
  pmsg(PMSG_INFO,format,args);
#endif
  va_end(args);
}

void pmsgSetLogStream(FILE *stream) {
  logStream = stream;
}
